package com.hs.panghu.dao.impl;

import com.hs.panghu.dao.ShopDao;
import com.hs.panghu.entity.Shop;
import com.hs.panghu.util.JDBCDruidUtil;
import sun.awt.geom.AreaOp;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class ShopDaoImpl implements ShopDao {
    @Override
    public int insertShop(Shop shop) {
        String sql="insert into shop(shopName, shopDescribe, shopImage," +
                "shopPhone, shopProvince, shopCity, shopDistrict," +
                "shopAddress, shopLongitude, shopLatitude, addTime)" +
                " values (?,?,?,?,?,?,?,?,?,?,now())";
        Object []object = {
                shop.getShopName(),
                shop.getShopDescribe(),
                shop.getShopImage(),
                shop.getShopPhone(),
                shop.getShopProvince(),
                shop.getShopCity(),
                shop.getShopDistrict(),
                shop.getShopAddress(),
                shop.getShopLongitude(),
                shop.getShopLatitude()
        };
        int row = JDBCDruidUtil.update(sql, object);
        return row;
    }

    @Override
    public int updateShop(Shop shop) {
        String sql="update shop set shopName=?,shopDescribe=?," +
                "shopImage=?,shopPhone=?,shopProvince=?,shopCity=?," +
                "shopDistrict=?,shopAddress=?,shopLongitude=?," +
                "shopLatitude=? where shopId=?";
        Object[]  object= {
                shop.getShopName(),
                shop.getShopDescribe(),
                shop.getShopImage(),
                shop.getShopPhone(),
                shop.getShopProvince(),
                shop.getShopCity(),
                shop.getShopDistrict(),
                shop.getShopAddress(),
                shop.getShopLongitude(),
                shop.getShopLatitude(),
                shop.getShopId()
        };
        int row=JDBCDruidUtil.update(sql,object);

        return row;
    }

    @Override
    public int deleteShop(Integer ShopId) {
        int row=0;

        return row;
    }

    @Override
    public List<Shop> selectShops(Map<String, String> map) {
        List<Shop>list=new ArrayList<>();
        StringBuilder sb=new StringBuilder("select*from shop where 1=1 ");
        List params=new ArrayList();


        //姓名
        String keyword = map.get("keyword");
        if (keyword!=null&&!"".equals(keyword)){
            sb.append(" and shopName like ? ");
            params.add("%"+keyword+"%");
        }
        //日期
        String startTime = map.get("startTime");
        String endTime = map.get("endTime");
        if (startTime!=null&&!"".equals(startTime)){
            sb.append(" and addTime between ? and ? ");
            params.add(startTime);
            params.add(endTime);
        }
        //状态
        String shopStatus=map.get("shopStatus");
        if (shopStatus!=null&&!"".equals(shopStatus)){
            sb.append(" and shopStatus =? ");
            params.add(shopStatus);
        }

        //手机号
        String shopPhone=map.get("phone");
        if (shopPhone!=null&&!"".equals(shopPhone)){
            sb.append(" and shopPhone like ? ");
            params.add("%"+shopPhone+"%");
        }
        //分页
        Integer pageNum = Integer.parseInt(map.get("pageNum"));

        Integer pageSize = Integer.parseInt(map.get("pageSize"));

        sb.append(" limit ?,? ");
        params.add((pageNum-1)*pageSize);
        params.add(pageSize);


        String sql =sb.toString();
        System.out.println(sql);
        Object[]objects=params.toArray();

        ResultSet query = JDBCDruidUtil.query(sql, objects);

        try {
            while (query.next()){
                Shop shop=new Shop(
                        query.getInt(1),
                        query.getString(2),
                        query.getString(3),
                        query.getString(4),
                        query.getString(5),
                        query.getString(6),
                        query.getString(7),
                        query.getString(8),
                        query.getString(9),
                        query.getString(10),
                        query.getString(11),
                        query.getInt(12),
                        query.getDate(13),
                        query.getInt(14),
                        query.getString(15),
                        query.getString(16),
                        query.getString(17),
                        query.getInt(18),
                        query.getString(19),
                        query.getString(20),
                        query.getString(21),
                        query.getInt(22),
                        query.getString(23),
                        query.getString(24)

                );
                list.add(shop);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JDBCDruidUtil.close();
        }


        return list;
    }

    @Override
    public int selectShopCount(Map<String, String> map) {
        int count=0;


        StringBuilder sb=new StringBuilder("select count(1) from shop where 1=1 ");
        List params=new ArrayList();


        //姓名
        String keyword = map.get("keyword");
        if (keyword!=null&&!"".equals(keyword)){
            sb.append(" and shopName like ? ");
            params.add("%"+keyword+"%");
        }
        //日期
        String startTime = map.get("startTime");
        String endTime = map.get("endTime");
        if (startTime!=null&&!"".equals(startTime)){
            sb.append(" and addTime between ? and ? ");
            params.add(startTime);
            params.add(endTime);
        }
        //状态
        String shopStatus=map.get("shopStatus");
        if (shopStatus!=null&&!"".equals(shopStatus)){
            sb.append(" and shopStatus =? ");
            params.add(shopStatus);
        }

        //手机号
        String shopPhone=map.get("phone");
        if (shopPhone!=null&&!"".equals(shopPhone)){
            sb.append(" and shopPhone like ? ");
            params.add("%"+shopPhone+"%");
        }


        String sql =sb.toString();
        Object[]objects=params.toArray();
        System.out.println(sql);
        ResultSet query = JDBCDruidUtil.query(sql, objects);

        try {
            while (query.next()){
                count=query.getInt(1);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JDBCDruidUtil.close();
        }
        return count;
    }
}
